using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class SpellEditorMenu : MonoBehaviour {
	
	public UILabel spellName;
	public UILabel schoolLabel;
	public UILabel castingTimeLabel;
	public UILabel componentsLabel;
	public UILabel rangeLabel;
	public UILabel durationLabel;
	public UILabel saveLabel;
	public UILabel descriptionLabel;
	public UICheckbox spellResistanceCheckBox;

	public GameObject CancelButton;
	public GameObject AcceptButton;
	
	public GameObject spellListAddButton;
	public GameObject spellListLabelPrefab;
	Dictionary<string,int> spellLists;
	
	public UITable holder;
	
	public GameObject target;
	public string functionName;
	
	string origName;
	
	static GameReference reference
	{
		get
		{
			return GameReference.reference;
		}
	}
	
	bool Loaded = false;

	// Use this for initialization
	void Start () {	
			UIButtonMessage message = spellListAddButton.AddComponent<UIButtonMessage>();
			message.target = gameObject;
			message.functionName = "SpellListAddButtonClicked";
		
		if(!Loaded)
		{			
			message = CancelButton.AddComponent<UIButtonMessage>();
			message.target = gameObject;
			message.functionName = "CancelButtonClicked";
			
			message = AcceptButton.AddComponent<UIButtonMessage>();
			message.target = gameObject;
			message.functionName = "AcceptButtonClicked";
			
			spellLists = new Dictionary<string, int>();
		}
		
		spellListLabelPrefab.SetActiveRecursively(false);
		holder.repositionNow = true;
	}
	
	public void LoadSpellDisplay(string sName, bool noEdit = false)
	{
		Debug.Log("Loading: " + sName);
		spellLists = new Dictionary<string, int>();
		
		Spell s = SpellManager.Load(sName);
		
		if(s == null)
		{
			SpellManager.DeleteSpell(sName);
			Destroy(gameObject);
			return;
		}
		
		origName = sName;
		
		spellName.text = s.name;
		schoolLabel.text = s.school;
		castingTimeLabel.text = s.castingTime;
		componentsLabel.text = s.components;
		rangeLabel.text = s.range;
		durationLabel.text = s.duration;
		saveLabel.text = s.save;
		spellResistanceCheckBox.isChecked = s.spellResistance;
		
		descriptionLabel.text = s.description;
		
		Loaded = true;
		
		if(s.spellList != null)
			CreateSpellListObjects(s.spellList, s.spellLevel);
		
		if(noEdit)//!isCustom && Application.platform != RuntimePlatform.WindowsEditor)
		{
			NGUITools.AddWidgetCollider(gameObject);
			
			foreach(Collider l in gameObject.GetComponentsInChildren<Collider>())
			{
				if(l.gameObject.name != "Content Panel")
					l.enabled = false;
			}
			
			collider.enabled = true;
			
			spellListAddButton.SetActiveRecursively(false);
			CancelButton.SetActiveRecursively(false);
			
			UIButtonMessage message = AcceptButton.AddComponent<UIButtonMessage>();		
			message.target = gameObject;
			message.functionName = "CancelButtonClicked";
			message.collider.enabled = true;
		}
		else
		{
			foreach(UIInput l in gameObject.GetComponentsInChildren<UIInput>())
			{
				l.text = l.label.text;
				l.mDefaultText = "Some Info";
			}
			
			UIButtonMessage message = AcceptButton.AddComponent<UIButtonMessage>();		
			message.target = gameObject;
			message.functionName = "SaveChangesClicked";
			
			CancelButton.GetComponentInChildren<UILabel>().text = "Delete!";
			message = CancelButton.AddComponent<UIButtonMessage>();		
			message.target = gameObject;
			message.functionName = "DeleteClicked";
		}
	}

	
	void CreateSpellListObjects(List<string> lists, List<int> levels)
	{
				
		for(int i = 0; i < lists.Count; i++)
		{
			spellLists.Add(lists[i], levels[i]);
			
			GameObject newSpellListLabel = Instantiate(spellListLabelPrefab) as GameObject;
			newSpellListLabel.transform.parent = holder.transform;
			newSpellListLabel.transform.localPosition = Vector3.zero;
			newSpellListLabel.transform.localScale = Vector3.one;
			newSpellListLabel.GetComponentsInChildren<UILabel>()[1].text = lists[i] + " " + levels[i];
			newSpellListLabel.name = "j. " + lists[i];
			UIButtonMessage message = newSpellListLabel.AddComponent<UIButtonMessage>();
			message.target = gameObject;
			message.functionName = "DeleteSpellListLabel";
		}
		
		holder.repositionNow = true;
	}
	
	IEnumerator SpellListAddButtonClicked()
	{
		List<SetterWindowDescription> descVar = new List<SetterWindowDescription>
		{
			new SetterWindowDescription("List", "Sorcerer/Wizard", true),
			new SetterWindowDescription("Level", "1", true),
		};
		
		yield return StartCoroutine(HelperFunctions.SetterWindow("Enter List and Level", descVar));
		
		if(descVar[0].outVariable != null)
		{
			int i;
			if(int.TryParse(descVar[1].outVariable, out i) && !spellLists.ContainsKey(descVar[0].outVariable))
			{
				spellLists.Add(descVar[0].outVariable, i);
				
				GameObject newSpellListLabel = Instantiate(spellListLabelPrefab) as GameObject;
				newSpellListLabel.transform.parent = holder.transform;
				newSpellListLabel.transform.localPosition = Vector3.zero;
				newSpellListLabel.transform.localScale = Vector3.one;
				newSpellListLabel.GetComponentsInChildren<UILabel>()[1].text = descVar[0].outVariable + " " + i;
				newSpellListLabel.name = "j. " + descVar[0].outVariable;
				UIButtonMessage message = newSpellListLabel.AddComponent<UIButtonMessage>();
				message.target = gameObject;
				message.functionName = "DeleteSpellListLabel";
			}
			
			holder.repositionNow = true;
		}
	}
	
	void DeleteSpellListLabel(GameObject target)
	{
		Debug.Log("Deleting: " + target);
		target.name = target.name.Remove(0,3);
		spellLists.Remove(target.name);
		Destroy(target);
		
		holder.repositionNow = true;
	}
	
	void SaveChangesClicked()
	{
		Spell s = new Spell();
		
		s.name = spellName.text;
		s.school = schoolLabel.text;
		s.castingTime = castingTimeLabel.text;
		s.components = componentsLabel.text;
		s.range = rangeLabel.text;
		s.duration = durationLabel.text;
		s.save = saveLabel.text;
		s.spellResistance = spellResistanceCheckBox.isChecked;
		
		s.description = descriptionLabel.text;
		
		s.spellList = new List<string>();
		s.spellLevel = new List<int>();
		s.spellList.AddRange(spellLists.Keys);
		s.spellLevel.AddRange(spellLists.Values);
		
		if(SpellManager.SaveSpell(s, origName))
		{
			target.SendMessage("renamedObject", new KeyValuePair<string, string>(origName, s.name), SendMessageOptions.DontRequireReceiver);
			
			Destroy(gameObject);
		}
	}
	
	void CancelButtonClicked()
	{
		Destroy(gameObject);
	}
	
	IEnumerator DeleteClicked()
	{
		List<bool> b = new List<bool>{false};
		
		yield return StartCoroutine(HelperFunctions.ConfirmationWindow("Are you sure you want to delete " + origName + "?", b));
		
		if(b[0])
		{
			SpellManager.DeleteSpell(origName);
			
			if(target != null)
				target.SendMessage(functionName);
		}

		Destroy(gameObject);
	}
	
	void AcceptButtonClicked()
	{
		Spell s = new Spell();
		
		s.name = spellName.text;
		s.school = schoolLabel.text;
		s.castingTime = castingTimeLabel.text;
		s.components = componentsLabel.text;
		s.range = rangeLabel.text;
		s.duration = durationLabel.text;
		s.save = saveLabel.text;
		s.spellResistance = spellResistanceCheckBox.isChecked;
		
		s.description = descriptionLabel.text;
		
		s.spellList = new List<string>();
		s.spellLevel = new List<int>();
		s.spellList.AddRange(spellLists.Keys);
		s.spellLevel.AddRange(spellLists.Values);
		
		s = SpellManager.AddSpell(s.name, s.school, s.castingTime, s.components, s.range, s.duration, s.save, s.spellResistance, s.spellList, s.spellLevel, s.description, false);
		
		if(target != null)
			target.SendMessage(functionName, new KeyValuePair<string,string>(s.name, s.description), SendMessageOptions.DontRequireReceiver);
		
		Destroy(gameObject);
	}
}
